package components.contorls.superClass
{
    import components.core.UIComponent;

    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.ColorMatrixFilter;

    import mx.events.FlexEvent;

    [Event(name = "selectionChange", type = "mx.events.FlexEvent")]
    public class BaseButton extends UIComponent
    {
        private var _label:String = "Button";

        private var _toggle:Boolean = false;

        private var _selected:Boolean = false;


        /**
         *是否缩放子对象
         */
        private var _isScale:Boolean = false;

        /**
         *子对象默认尺寸列表
         */
        private var _defalutSizeArray:Array = [];

        /**
         *自己能否设置为选中状态
         */
        private var _selfSelectedEnabel:Boolean = true;

        private var _color:Number = 0x0;

        private var _align:String = "center";

        private var _size:uint = 12;

        private var _bold:Boolean = false;

        private var _fontName:String = "宋体";

        public function BaseButton()
        {
            super();
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get labelTF():BaseLabel
        {
            return getChildByName("labelSkin") as BaseLabel;
        }

        public function get upDisplay():Sprite
        {
            return getChildByName("upSkin") as Sprite;
        }

        public function get overDisplay():Sprite
        {
            return getChildByName("overSkin") as Sprite;
        }

        public function get downDisplay():Sprite
        {
            return getChildByName("downSkin") as Sprite;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/
        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized)
            {
                if (labelTF)
                {
                    labelTF.left = labelTF.x;
                    labelTF.right = _normalWidth - labelTF.left - labelTF.width;
                    labelTF.verticalCenter = 0;

                    labelTF.mouseChildren = false;
                    labelTF.mouseEnabled = false;
                }

                upDisplay.mouseChildren = upDisplay.mouseEnabled = false;
                overDisplay.mouseChildren = overDisplay.mouseEnabled = false;
                downDisplay.mouseChildren = downDisplay.mouseEnabled = false;

                _defalutSizeArray.push(upDisplay.width);
                _defalutSizeArray.push(upDisplay.height);
                _defalutSizeArray.push(overDisplay.width);
                _defalutSizeArray.push(overDisplay.height);
                _defalutSizeArray.push(downDisplay.width);
                _defalutSizeArray.push(downDisplay.height);
            }
            if (labelTF)
            {
                labelTF.text = label;
                labelTF.color = color;
                labelTF.align = align;
                labelTF.size = size;
                labelTF.bold = bold;
                labelTF.fontName = fontName;
            }
        }

        protected override function measureSizes():Boolean
        {
            var change:Boolean = super.measureSizes();

            if (labelTF)
                labelTF.invalidate();

            if (_isScale)
            {
                upDisplay.width = overDisplay.width = downDisplay.width = width;
                upDisplay.height = overDisplay.height = downDisplay.height = height;
            }
            else
            {
                upDisplay.width = Number(_defalutSizeArray[0]);
                upDisplay.height = Number(_defalutSizeArray[1]);

                overDisplay.width = Number(_defalutSizeArray[2]);
                overDisplay.height = Number(_defalutSizeArray[3]);

                downDisplay.width = Number(_defalutSizeArray[4]);
                downDisplay.height = Number(_defalutSizeArray[5]);
            }

            return change;
        }

        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        [Inspectable(defaultValue = "Button")]
        public function get label():String
        {
            return _label;
        }

        public function set label(value:String):void
        {
            _label = value;
            invalidate();
        }

        [Inspectable(defaultValue = "false")]
        public function get toggle():Boolean
        {
            return _toggle;
        }

        public function set toggle(value:Boolean):void
        {
            _toggle = value;

            if (!value)
                selected = false;
        }

        [Inspectable(defaultValue = "false")]
        public function get selected():Boolean
        {
            return _selected;
        }

        public function set selected(value:Boolean):void
        {
            if (selected == value)
                return;

            if (!toggle)
            {
                _selected = false;
                return;
            }
            _selected = value;

            setOutState();

            invalidate();

            dispatchEvent(new FlexEvent(FlexEvent.SELECTION_CHANGE));
        }

        [Inspectable(defaultValue = "false")]
        public function get isScale():Boolean
        {
            return _isScale;
        }

        public function set isScale(value:Boolean):void
        {
            _isScale = value;
            invalidate();
        }

        [Inspectable(defaultValue = "true")]
        public function get selfSelectedEnabel():Boolean
        {
            return _selfSelectedEnabel;
        }

        public function set selfSelectedEnabel(value:Boolean):void
        {
            _selfSelectedEnabel = value;
        }

        [Inspectable(defaultValue = "center", type = "List", enumeration = "left,center,right")]
        public function get align():String
        {
            return _align;
        }

        public function set align(value:String):void
        {
            _align = value;

            invalidate();
        }

        [Inspectable(defaultValue = "#000000", type = "Color")]
        public function get color():Number
        {
            return _color;
        }

        public function set color(value:Number):void
        {
            _color = value;

            invalidate();
        }

        [Inspectable(defaultValue = "12")]
        public function get size():uint
        {
            return _size;
        }

        public function set size(value:uint):void
        {
            _size = value;

            invalidate();
        }

        [Inspectable(defaultValue = "false")]
        public function get bold():Boolean
        {
            return _bold;
        }

        public function set bold(value:Boolean):void
        {
            _bold = value;

            invalidate();
        }

        [Inspectable(defaultValue = "宋体")]
        public function get fontName():String
        {
            return _fontName;
        }

        public function set fontName(value:String):void
        {
            _fontName = value;

            invalidate();
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        protected override function frameConstructedHandler(event:Event):void
        {
            super.frameConstructedHandler(event);

            setOutState();

            buttonMode = true;
            overGlow = true;

            if (labelTF)
                labelTF.mouseEnabled = labelTF.mouseChildren = false;
        }


        protected override function rollOverHandler(event:MouseEvent):void
        {
            super.rollOverHandler(event);

            setOverState();
        }


        protected override function rollOutHandler(event:MouseEvent):void
        {
            super.rollOutHandler(event);
            setOutState();
        }

        protected override function mouseDownHandler(event:MouseEvent):void
        {
            super.mouseDownHandler(event);

            setDownState();
        }

        protected override function mouseUpHandler(event:MouseEvent):void
        {
            super.mouseUpHandler(event);

            setUpState();
        }

        protected override function mouseClickHandler(event:MouseEvent):void
        {
            super.mouseClickHandler(event);
            if (toggle && selfSelectedEnabel)
                selected = !selected;
        }

        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;

            if (labelTF)
                labelTF.focusComp = focusComp;
        }

        public override function destroy():void
        {
            super.destroy();

            _label = "";
        }

        public override function validateNow():void
        {
            super.validateNow();
            labelTF.validateNow();
        }


        public function setOverState():void
        {
            if (selected)
            {
                setDownState();
            }
            else
            {
                upDisplay.visible = false;
                overDisplay.visible = true;
                downDisplay.visible = false;
            }
        }


        public function setOutState():void
        {
            if (selected)
            {
                setDownState();
            }
            else
            {
                upDisplay.visible = true;
                overDisplay.visible = false;
                downDisplay.visible = false;
            }
        }


        public function setDownState():void
        {
            upDisplay.visible = false;
            overDisplay.visible = false;
            downDisplay.visible = true;
        }


        public function setUpState():void
        {
            if (selected)
                setDownState();
            else
            {
                if (!isOver)
                {
                    upDisplay.visible = true;
                    overDisplay.visible = false;
                    downDisplay.visible = false;
                }
                else
                {

                    upDisplay.visible = false;
                    overDisplay.visible = true;
                    downDisplay.visible = false;
                }
            }
        }
    }
}
